﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using DTOFrameworks;
using System.ComponentModel;
using FooFun.DataElements.Dtos;

namespace FooFun.Web.Controls
{
    [ParseChildren(true), PersistChildren(false)]
    public class ReversalTree : Control, IDataElementBinder
    {
        public ReversalTree()
        {

        }

        #region IDataElementBinder 成员
        IDataElement dataElement;
        public IDataElement DataElement
        {
            get
            {
                return dataElement;
            }
            set
            {
                dataElement = value;
            }
        }

        #endregion

        //ITemplate ItemTemplate
        [
            Browsable(false),
            PersistenceMode(PersistenceMode.InnerProperty),
            Description("普通项模板"),
            TemplateContainer(typeof(DataElementWebControl))
        ]
        public ITemplate ItemTemplate { get; set; }

        [
            Browsable(false),
            PersistenceMode(PersistenceMode.InnerProperty),
            Description("根数据项模板"),
            TemplateContainer(typeof(DataElementWebControl))
        ]
        public ITemplate RootTemplate { get; set; }

        [
            Browsable(false),
            PersistenceMode(PersistenceMode.InnerProperty),
            Description("第一条数据项模板"),
            TemplateContainer(typeof(DataElementWebControl))
        ]
        public ITemplate FirstTemplate { get; set; }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            if (this.ItemTemplate == null)
            {
                this.ChildControlsCreated = true;
                return;
            }

            IDataElement currentElement = this.DataElement;
            if (FirstTemplate != null)
            {
                firstControl
                    = new DataElementWebControl();
                firstControl.DataElement = currentElement;

                FirstTemplate.InstantiateIn(firstControl);

                currentElement = currentElement.Parent;

                this.Controls.AddAt(0, firstControl);

                firstControl.DataBind();
            }

            while (currentElement != null && !currentElement.IsEmpty)
            {
                IDataElement parentElement = currentElement.Parent;

                // 最后一个数据项
                if (parentElement == null || parentElement.IsEmpty)
                {
                    if (RootTemplate != null)
                    {
                        DataElementWebControl control
                    = new DataElementWebControl();
                        control.DataElement = currentElement;

                        RootTemplate.InstantiateIn(control);

                        this.Controls.AddAt(0, control);

                        control.DataBind();

                        break;
                    }
                    else
                    {
                        DataElementWebControl control
                            = new DataElementWebControl();
                        control.DataElement = currentElement;

                        ItemTemplate.InstantiateIn(control);

                        this.Controls.AddAt(0, control);

                        control.DataBind();
                    }
                }
                else
                {
                    DataElementWebControl control
                        = new DataElementWebControl();
                    control.DataElement = currentElement;

                    ItemTemplate.InstantiateIn(control);

                    this.Controls.AddAt(0, control);

                    control.DataBind();
                }
                currentElement = parentElement;
            }

            this.ChildControlsCreated = true;
        }

        DataElementWebControl firstControl;

        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }
    }
}
